package com.cc.controller;

import com.cc.model.EbOrder;
import com.cc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
   @Autowired
    private OrderService orderService;

//    @RequestMapping("to_save")
//    public String toSave(){
//        return "shopping";
//    }
//
//    @RequestMapping("/save_commit")
//    public String saveCommit(EbOrder ebOrder){
//        System.out.println("ebOrder"+ebOrder);
//        //补充添加时间
//        //获取系统时间
//        Date date = new Date();
//        //Date---->String
//        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
//        String timeStr = sdf.format(date);
//        //System.out.println("timeStr=="+timeStr);
//        ebOrder.setEoCreateTime(timeStr);
//        //1.获取表单的值---参数，参数校验
//        if((ebOrder.getEoUserId()==null||
//                ebOrder.getEoUserId().equals("")||
//                ebOrder.getEoUserId().trim().length()==0)
//                ||(ebOrder.getEoUserName()==null||
//                ebOrder.getEoUserName().equals("")||
//                ebOrder.getEoUserName().trim().length()==0)
//                ||(ebOrder.getEoUserAddress()==null||
//                ebOrder.getEoUserAddress().equals("")||
//                ebOrder.getEoUserAddress().trim().length()==0)
//                ||(ebOrder.getEoCreateTime()==null||
//                ebOrder.getEoCreateTime().equals("")||
//                ebOrder.getEoCreateTime().trim().length()==0)
//                ||(ebOrder.getEoCost()<0)
//                || (ebOrder.getEoStatus()<1||
//                ebOrder.getEoStatus()>5)
//                || (ebOrder.getEoType()<1||
//                ebOrder.getEoType()>2 )){
//            return "shopping-error";//error.jsp
//        }
//
//        //2.调service保存的方法
//        int result = orderService.save(ebOrder);
//        if(result<=0){
//            //return或异常
//        }
//        return "shopping-result";
//    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        //1.参数校验

        //2.执行删除
        int result = orderService.deleteByPk(id);
        if(result==0){
            //TODO 要么return错误页面 要么抛出异常
        }
        return "manage/manage-result";
    }

    @RequestMapping("/edit")
    public String edit(Model model,Integer eoId){

        //1.根据订单的编号查询订单的详细信息
        EbOrder ebOrder = orderService.selectByCondition(eoId);

        //2.将查出来的订单信息传递给order-modify.jsp
        model.addAttribute("ebOrder",ebOrder);
        return "manage/order-modify";
    }

    @RequestMapping("/editSubmit")
    public String editSubmit(EbOrder ebOrder){

        //1.更新订单信息
        int result = orderService.updateById(ebOrder);
        //2.跳转到更新结果页面
        System.out.println("这里是result的值"+result);
        if(result <= 0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1")int page,
                             @RequestParam(value = "pageSize",defaultValue = "3")int pageSize) {
        //1.获取新闻表的第一页数据，每页三条
        List<EbOrder> orders = orderService.list(page,pageSize);
        int maxPage = orderService.getMaxPage(pageSize);
        //2.将数据（list） 和视图（jsp）返回String
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("orders",orders);
        //绑定视图
        mv.addObject("curPage",page);

        mv.addObject("maxPage",maxPage);

        mv.setViewName("manage/order");
        return mv;
    }
}
